package com.store.app;


import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.store.app.http.Stores;
import com.store.app.hyphenate.HyphenateHelper;
import com.store.app.stores.LocationService;
import com.store.app.utils.SettingData;
import com.umeng.socialize.PlatformConfig;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * 主Application，所有百度定位SDK的接口说明请参考线上文档：http://developer.baidu.com/map/loc_refer/index.html
 * <p/>
 * 百度定位SDK官方网站：http://developer.baidu.com/map/index.php?title=android-locsdk
 * <p/>
 * 直接拷贝com.baidu.location.service包到自己的工程下，简单配置即可获取定位结果，也可以根据demo内容自行封装
 */
public class LocationApplication extends Application {

    public LocationService locationService;
    public Vibrator mVibrator;
    public static LocationApplication app;
    public boolean debugMode = false;//默认true为测试模式，为false为正式开发模式

    public static SoundPool soundPool;
    private static int music;
    static List<String> data = new ArrayList<String>();//保存全局消息

    public static List<String> getData() {
        return data;
    }

    public static String im_user_id = "";

    public static void setData(List<String> data) {
        LocationApplication.data = data;
    }
    public  DisplayMetrics dm;
    public static String  shareURL = "";
    public static String userId;//目前给收银时候收银记录的用或者绑定淘宝时候会用




    public  DisplayMetrics getDM(){
        if(dm==null){
            dm = app.getResources().getDisplayMetrics();
        }
        return dm;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        if (!TextUtils.isEmpty(preferences.getString("im_user_id", ""))) {
            im_user_id = preferences.getString("im_user_id", "");
            Log.v("zyl","LocationApplication 获取到了im_user_id："+im_user_id);
        }else{
            Log.v("zyl","LocationApplication 获取不到im_user_id");
        }


        /***
         * 初始化定位sdk，建议在Appdlication中创建
         */
        app = this;
        locationService = new LocationService(getApplicationContext());
        mVibrator = (Vibrator) getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
//        WriteLog.getInstance().init(); // 初始化日志
        //初始化百度地图
        SDKInitializer.initialize(getApplicationContext());
        initImageLoader(getApplicationContext());

        soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
        music = soundPool.load(this, R.raw.qrcode_completed, 1); //把你的声音素材放到res/raw里，第2个参数即为资源文件，第3个为音乐的优先级
//        soundPool.play(music, 1, 1, 0, 0, 1); 二维码调用
        //测试环境
        if (debugMode) {
//            Stores.http = "http://172.16.30.82:8080";
            Stores.http = "https://t-appportal.meitianhui.com";
            SettingData.http = "https://t-appportal.meitianhui.com";
//            Stores.http = "http://192.168.16.18:3001";
//            SettingData.http = "http://192.168.16.18:3001";
//            Stores.http = "http://192.168.16.70:80";
//            SettingData.http = "http://192.168.16.70:80";
//            SettingData.hydURL = "http://192.168.16.53/wap";
//            SettingData.hydPayURL = "http://192.168.16.53";
//            SettingData.hydURL = "http://d.meitianhui.com/wap";
//            SettingData.http = "http://192.168.16.63:3001";
            SettingData.app_id = "bcc1cf2e-b376-11e5-921c-fcaa1490ccaf";
            SettingData.private_key = "690b98fcb37711e5921cfcaa1490ccaf717130d1b37711e5921cfcaa1490ccaf";
            SettingData.hydURL = "http://s.meitianhui.com/wap";
//            SettingData.hydURL = "http://s.meitianhui.com";
//            SettingData.hydURL = "http://192.168.16.254/wap";
            SettingData.registNewStoresURL = "http://test-mps.meitianhui.com/omp-mth-productSpecialist/mobile/stores/app/register";
//            SettingData.registNewStoresURL = "http://192.168.16.60:8080/omp-mth-productSpecialist/mobile/stores/app/register";
//            SettingData.hydURL = "http://new.meitianhui.com/wap";
            shareURL = "http://121.43.59.219/fan/wap?area_id=";
        } else {
            SettingData.registNewStoresURL = "https://mps.meitianhui.com/omp-mth-productSpecialist/mobile/stores/app/register";
            Stores.http = "https://appportal.meitianhui.com";
            SettingData.http = "https://appportal.meitianhui.com";
            SettingData.app_id = "2b4a83a8-296b-4b6c-8601-dd72e50bdda5";
            SettingData.private_key = "c60fa6d4556a43ba98177b3c290e62e0c701d8e586b344448aed736563298118";
            SettingData.hydURL = "http://d.meitianhui.com/wap";
//            http://t-appportal.meitianhui.com/openapi/h5/b/course
            shareURL = "https://fan.meitianhui.com/fan/wap?area_id=";
        }
        if(null==Stores.area_id){
            Stores.area_id  = "";
        }
        shareURL  += Stores.area_id;//在分享地址后面加上地区id
        Log.v("zyl", "开始复制http:" + SettingData.http);
        initShareSDK();
        //init demo helper
        HyphenateHelper.getInstance().init(this);
    }

    private void initShareSDK(){
        PlatformConfig.setWeixin("wxd3edf71db8627648", "b38f3986d6cb5d1e10cf6e2e594e778a");
        //微信 appid appsecret
//        PlatformConfig.setSinaWeibo("3921700954","04b48b094faeb16683c32669824ebdad");
        //新浪微博 appkey appsecret
        PlatformConfig.setQQZone("1105363026", "vk0XiF7mdpsGFohI");
        // QQ和Qzone appid appkey
    }

    /**
     * 播放扫描二维码的声音
     */
    public static void scanSound() {
        soundPool.play(music, 1, 1, 0, 0, 1);
    }

    public static void showTostToken(){
//        Toast.makeText(app,"您的账号已在其他手机退出，请重新登录！",Toast.LENGTH_LONG).show();
    }

    /**
     * 初始化ImageLoader图片加载框架
     * @param context
     */
    public static void initImageLoader(Context context) {

        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs();
        ImageLoader.getInstance().init(config.build());

    }

    /**
     * 检测当前网络是否可用
     * @return
     */
    public static boolean isNetworkAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) app
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected())
            {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED)
                {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }
}
